import Vue from 'vue'

export default ({ redirect, $axios, app }) => {
    $axios.defaults.baseURL = 'http://lianpeiwang.ip189.lhyw.net/'
    // $axios.defaults.baseURL = 'http://glc.ilianpei.com/'
    // $axios.defaults.baseURL = process.env.BASE_URL

    $axios.onRequest(config => {
        // console.log('刚开始', config)
        // 向参数中塞入 token
        let data = {}
        if (config.method.toUpperCase() === 'GET') {
            data = config.params || {}
            config.params = data
        } else {
            // POST 需要塞入 data
            data = config.data || {}
            config.data = data
        }
        // console.log('开始了', config)
    });

    $axios.onResponse(res => {
        // 返回数据逻辑处理
        // console.log('成功', res)
        if (res.data.code == 400001 || res.data.code == 400002 || res.data.code == 400003 || res.data.code == 400004) {
            Vue.$toast('您未登录或登录已过期')
            clearCookie()

            // 登录成功后跳转地址
            localStorage.setItem('returnPath', window.location.href)

            let origin = window.location.origin
            window.location.href = origin + "/recruit/login"
            Vue.$router.push({
                name: "recruit-login"
            });
            return false
        } else if (res.data.code == 400005) {
            // 账号冻结
            Vue.$toast(res.data.msg)
            clearCookie()

            let origin = window.location.origin
            window.location.href = origin
            Vue.$router.push({
                name: "index"
            });
            return false
        } else if (res.config.url == '/wap/index/good/recommend' || res.config.url == '/wap/index/flagship/store') {
            return false
        } else if (res.data.code != 0) {
            Vue.$toast(res.data.msg)
            return false
        }
        let tokenObj = ['/wap/login/index', '/wap/register/phone', '/wap/register/weChat', '/wap/register/doneInfo']
        for (let i in tokenObj) {
            if (res.config.url == tokenObj[i]) {
                window.localStorage.removeItem('token')
                window.localStorage.setItem('token', res.data.data.token)
                document.cookie = 'TOKEN=' + res.data.data.token + '; path=/'
            }
        }
        let qxObj = ["/home/register/phone", "/home/login/phone_password"]
        for (let w in qxObj) {
            if (res.config.url == qxObj[w]) {
                window.localStorage.removeItem('qx_token')
                window.localStorage.setItem('qx_token', res.data.data.token)
            }
        }
    });

    $axios.onError(error => {
        console.log('失败了')
    })
};

// 不能全部清除缓存，有登录成功之后判断跳转地址。
function clearCookie() {
    window.localStorage.removeItem('phone')
    window.localStorage.removeItem('token')
    window.localStorage.removeItem('user_id')
    window.localStorage.removeItem('qx_token')
    window.localStorage.removeItem('qx_phone')
    window.localStorage.removeItem('qx_id')
    window.localStorage.removeItem('vid')
    window.localStorage.removeItem('friend_type')
    window.localStorage.removeItem('is_perfect')


    window.localStorage.removeItem('openid')
    window.localStorage.removeItem('user_name')
    window.localStorage.removeItem('head_img')

    window.localStorage.removeItem('goods_id')
    window.localStorage.removeItem('goods_id_type')
}